package com.access;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.beans.LinkBean;
import org.htmlparser.beans.StringBean;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.jsoup.Connection;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Extractor {

	private String title;
	private String lastModified;
	private String content;
	private ArrayList<String> childLinks;

	public String getTitle() {
		return title;
	}

	public String getLastModified() {
		return lastModified;
	}

	public String getContent() {
		return content;
	}

	public ArrayList<String> getChildLinks() {
		return childLinks;
	}

	public Extractor(String url) throws IOException {

		title = null;
		lastModified = null;
		content = null;
		childLinks = new ArrayList<String>();

		Connection connection = HttpConnection.connect(url);

		Document document = connection.get();

		Elements links = document.select("a");
		title = document.title();
		lastModified = connection.response().header("Last-Modified");
		content = document.body().text();
		for (Element link : links) {
			childLinks.add(link.attr("abs:href"));
		}

	}

	// extract child links excluding itself
	public static ArrayList<String> extractChildLinks(String url) {

		LinkBean linkBean = new LinkBean();
		linkBean.setURL(url);
		URL[] childLinks = linkBean.getLinks();
		ArrayList<String> children = new ArrayList<String>();
		for (int i = 0; i < childLinks.length; i++) {
			children.add(childLinks[i].toString());
		}
		return children;
	}

	public static String extractContent(String url) {

		StringBean stringBean;
		stringBean = new StringBean();
		stringBean.setLinks(false);
		stringBean.setURL(url);
		return stringBean.getStrings();

	}

	public static String extractTitle(String url) throws ParserException {

		Parser parser = new Parser();
		parser.setURL(url);
		NodeList nodeList = parser.parse(new TagNameFilter("title"));
		Node node = nodeList.elementAt(0);
		if (node != null) {
			String result = node.toString();
			if (result.length() > 7) {
				return result.substring(7);
			}
		}
		return null;

	}

	public static Date extractLastModifiedDate(String url) throws IOException {

		URLConnection connection = new URL(url).openConnection();
		connection.connect();
		long lastModified = connection.getLastModified();
		if (lastModified == 0) {
			return null;
		} else {
			return new Date(lastModified);
		}
	}

}
